from rest_framework.response import Response


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    :param token:  认证成功之后,系统生成的token
    :param user:   认证登陆之后,的user对象
    :param request:     登陆认证的请求
    :return:
    """
    return {
        'token': token,
        'id': user.id,
        'username': user.username
    }


from rest_framework.pagination import PageNumberPagination
# 自定义分页器,查询用户时定义的
class PageNum(PageNumberPagination):

    page_size = 4 # 指定后端显示页容量,没有设置这个就不分页

    # 默认是None,要用pagesize,就要配置page_size_query_param
    page_size_query_param = "pagesize"

    max_page_size = 20

    # 重写分页返回结果方法
    def get_paginated_response(self, data):
        # return Response(OrderedDict([
        #     ('count', self.page.paginator.count),
        #     ('next', self.get_next_link()),
        #     ('previous', self.get_previous_link()),
        #     ('results', data)
        # ]))

        return Response({
            "count":self.page.paginator.count,
            "lists":data,# 返回结果就是data,原来是results
            "page":self.page.number,#　页码
            "pages":self.page.paginator.num_pages,#　总页数
            "pagesize":self.max_page_size # 指定页容量
        })


